<?php
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;
use app\common\request\Curl;
use app\common\request\ClientLai;
use app\common\model\CarsInfo;
use app\common\model\CarsTemp;

class Compare extends Command
{
	protected function configure()
    {
        $this->setName('compare')->setDescription('compare');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->contrast();
    	$output->writeln('success');
    }

    public function contrast(){
    	$cars_w = Db::name('cars')->where('supplier_no',1376920001)->where('delete_time',null)->count();
    	$cars_s = Db::name('cars')->where('supplier_no',1376920002)->where('delete_time',null)->count();
    	$cars_iot = Db::name('cars')->where('supplier_no',1376920003)->where('delete_time',null)->count();

    	$info_w = Db::name('cars_info')->where('supplier_no',1376920001)->count();
    	$info_s = Db::name('cars_info')->where('supplier_no',1376920002)->count();
    	$info_iot = Db::name('cars_info')->where('supplier_no',1376920003)->count();

    	if($cars_w == $info_w && $cars_s == $info_s && $cars_iot == $info_iot){
    		return true;
    	}else{
    		$carsData = Db::name('cars_info')->where('supplier_no','NEQ','')->select();
    		$cars_temp = new CarsTemp();
            $cars_temp->execute('TRUNCATE `joy_cars_temp`');
            $temp = $cars_temp->insertAll($carsData);

            if($temp){
            	$data = Db::name('cars')->where('supplier_no','NEQ','')->where('delete_time',null)->field('car_unique_id,supplier_no')->select();
           		$cars_info = new CarsInfo();
	            $cars_info->execute('TRUNCATE `joy_cars_info`');
	            $infoData = $cars_info->insertAll($data);
	            if($infoData){
	            	Db::name('cars_temp')->chunk(100, function($info) {
					    foreach ($info as $dataum) {
					        if($dataum['network_status'] === 0 || $dataum['network_status'] === 1){
					        	$model = new CarsInfo;
					        	$model->save($dataum,['car_unique_id' =>$dataum['car_unique_id']]);
					        }
					    }
					});
	            }
            }
    	}
    }

} 